using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Serialization;

namespace TicTacToe
{
    public class OptionSaver
    {
        public static readonly string BOARD_SIZE = "BOARD_SIZE";
        public static readonly string PLAY_FIRST = "PLAY_FIRST";
        public static readonly string X_O_STYLE = "X_O_STYLE";
        public static readonly string NUM_TO_WIN = "NUM_TO_WIN";

        private static readonly string FILE_NAME = "option_.xml";
        private static XmlDictionary<string, string> option = Load();

        private static XmlDictionary<string, string> Load()
        {
            XmlDictionary<string, string> optionTemp = null;
            TextReader reader = null;
            try
            {
                IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
                if (isoStorage.FileExists(FILE_NAME))
                {
                    IsolatedStorageFileStream file = isoStorage.OpenFile(FILE_NAME, FileMode.Open);
                    XmlSerializer xs = new XmlSerializer(typeof(XmlDictionary<string, string>));

                    reader = new StreamReader(file);
                    optionTemp = (XmlDictionary<string, string>)xs.Deserialize(reader);
                    reader.Close();
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
            }

            return optionTemp;
        }

        public static void Save(XmlDictionary<string, string> option)
        {
            TextWriter writer = null;
            try
            {
                IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream file = isoStorage.OpenFile(FILE_NAME, FileMode.Create);
                XmlSerializer xs = new XmlSerializer(typeof(XmlDictionary<string, string>));
                writer = new StreamWriter(file);
                xs.Serialize(writer, option);
                writer.Close();
            }
            finally
            {
                if (writer != null)
                {
                    writer.Dispose();
                }
            }
        }

        public static object Get(string key)
        {
            //IsolatedStorageFile.GetUserStoreForApplication().DeleteFile(FILE_NAME);

            option = Load();

            if (option == null)
            {
                // for the first run, assign default option
                option = new XmlDictionary<string, string>();
                option.Add(BOARD_SIZE, BoardSize.Three_x_Three.ToString());
                option.Add(PLAY_FIRST, TicTacToePlayer.PlayerX.ToString());
                option.Add(X_O_STYLE, Style_Texture.Classic.ToString());
                option.Add(NUM_TO_WIN, "3");
                Save(option);
            }
            return option[key];
        }

        public static void Set(string key, object value)
        {
            option = Load();

            if (option == null)
            {
                // for the first run, assign default option
                option = new XmlDictionary<string, string>();
                option.Add(BOARD_SIZE, BoardSize.Three_x_Three.ToString());
                option.Add(PLAY_FIRST, TicTacToePlayer.PlayerX.ToString());
                option.Add(X_O_STYLE, Style_Texture.Classic.ToString());
                option.Add(NUM_TO_WIN, "3");
            }

            option[key] = value.ToString();
            OptionSaver.Save(option);
        }
    }
}
